package pers.qianyu.month_202012.date_20201212;

import org.junit.*;

import java.util.*;

/**
 * 739. 每日温度
 * https://leetcode-cn.com/problems/daily-temperatures/
 *
 * @author mizzle rain
 * @date 2020-12-12 17:26
 */
public class DailyTemperatures {
    public int[] dailyTemperatures(int[] T) {
        int[] res = new int[T.length];
        Stack<Integer> s = new Stack<>();
        for (int i = T.length - 1; i >= 0; i--) {
            while (!s.isEmpty() && T[s.peek()] <= T[i]) {
                s.pop();
            }
            res[i] = s.isEmpty() ? -1 : s.peek();
            s.push(i);
        }
        for (int i = 0; i < res.length; i++) {
            res[i] = res[i] == -1 ? 0 : res[i] - i;
        }
        return res;
    }

    @Test
    public void test1() {
        int[] arr = {73, 74, 75, 71, 69, 72, 76, 73};
        int[] result = new DailyTemperatures().dailyTemperatures(arr);
        System.out.println("result = " + Arrays.toString(result));
    }
}
